let MS = require("./ms.js")
const request = require("./request.js")
const Error = require('./error')
const storage = require("./storage.js")

const {
    isObject,
    isString,
    isNumber,
    isUndefined,
    isBoolean,
    isArray
} = require('./dataType')

const customerInfo = class customerInfo {

    constructor() {}

    query(options) {
        let params = {
            pageNum: 1,
            pageSize: 10
        }

        if(!isUndefined(options.searchCode) && isString(options.searchCode)) {
            params.searchCode = options.searchCode;
        }
        if(!isUndefined(options.userId) && isString(options.userId)) {
            params.userId = options.userId;
        }
        if(!isUndefined(options.startTime) && isString(options.startTime)) {
            params.startTime = options.startTime;
        }
        if(!isUndefined(options.endTime) && isString(options.endTime)) {
            params.endTime = options.endTime;
        }
        if(!isUndefined(options.gender) && isNumber(options.gender)) {
            params.gender = options.gender;
        }
        if(!isUndefined(options.category) && isNumber(options.category)) {
            params.category = options.category;
        }
        if(!isUndefined(options.pageNum) && isNumber(options.pageNum)) {
            params.pageNum = options.pageNum;
        }
        if(!isUndefined(options.pageSize) && isNumber(options.pageSize)) {
            params.pageSize = options.pageSize;
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.CUSTOMER_INFO.LOAD_CUSTOMER_INFO.url,
                MS._config.parameters.CUSTOMER_INFO.LOAD_CUSTOMER_INFO.method,params).then(res => {
                    resolve(res.data);
            }).catch(err => {
                reject(err);
            })
        })
    }

    get(id) {
        let data = {customerId: id}
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.CUSTOMER_INFO.GET.url,
                MS._config.parameters.CUSTOMER_INFO.GET.method, data).then(res => {
                    resolve(res.data);
            }).catch(err => {
                reject(err);
            })
        })
    }

    userApply(options) {
        let params = {}
        if(!isUndefined(options.customerName) && isString(options.customerName)) {
            params.customerName = options.customerName;
        }
        if(!isUndefined(options.email) && isString(options.email)) {
            params.email = options.email;
        }
        if(!isUndefined(options.wechat) && isString(options.wechat)) {
            params.wechat = options.wechat;
        }
        if(!isUndefined(options.phone) && isString(options.phone)) {
            params.phone = options.phone;
        }
        if(!isUndefined(options.age) && isNumber(options.age)) {
            params.age = options.age;
        }
        if(!isUndefined(options.birthday) && isString(options.birthday)) {
            params.birthday = options.birthday;
        }
        if(!isUndefined(options.gender) && isNumber(options.gender)) {
            params.gender = options.gender;
        }
        if(!isUndefined(options.occupation) && isString(options.occupation)) {
            params.occupation = options.occupation;
        }
        if(!isUndefined(options.customerId) && isString(options.customerId)) {
            params.customerId = options.customerId;
        }
        if(!isUndefined(options.customerOpenId) && isString(options.customerOpenId)) {
            params.customerOpenId = options.customerOpenId;
        }
        if(!isUndefined(options.longitude) && isNumber(options.longitude)) {
            params.longitude = options.longitude;
        }
        if(!isUndefined(options.latitude) && isNumber(options.latitude)) {
            params.latitude = options.latitude;
        }
        if(!isUndefined(options.customerUnionId) && isString(options.customerUnionId)) {
            params.customerUnionId = options.customerUnionId;
        }
        if(!isUndefined(options.country) && isString(options.country)) {
            params.country = options.country;
        }
        if(!isUndefined(options.province) && isString(options.province)) {
            params.province = options.province;
        }
        if(!isUndefined(options.city) && isString(options.city)) {
            params.city = options.city;
        }
        if(!isUndefined(options.intentionLevel) && isNumber(options.intentionLevel)) {
            params.intentionLevel = options.intentionLevel;
        }
        if(!isUndefined(options.needType) && isNumber(options.needType)) {
            params.needType = options.needType;
        }
        return new Promise((resolve, reject) =>{
            request(MS._config.parameters.CUSTOMER_INFO.USER_APPLY.url,
                MS._config.parameters.CUSTOMER_INFO.USER_APPLY.method, params).then(res => {
                resolve(res.data)
            }).catch(err =>{
                reject(err)
            })
        })
    }

    employeeEntry(options) {
        let params = {}
        if(!isUndefined(options.gathererId) && isString(options.gathererId)) {
            params.gathererId = options.gathererId;
        }
        if(!isUndefined(options.customerName) && isString(options.customerName)) {
            params.customerName = options.customerName;
        }
        if(!isUndefined(options.email) && isString(options.email)) {
            params.email = options.email;
        }
        if(!isUndefined(options.wechat) && isString(options.wechat)) {
            params.wechat = options.wechat;
        }
        if(!isUndefined(options.phone) && isString(options.phone)) {
            params.phone = options.phone;
        }
        if(!isUndefined(options.age) && isNumber(options.age)) {
            params.age = options.age;
        }
        if(!isUndefined(options.birthday) && isString(options.birthday)) {
            params.birthday = options.birthday;
        }
        if(!isUndefined(options.gender) && isNumber(options.gender)) {
            params.gender = options.gender;
        }
        if(!isUndefined(options.occupation) && isString(options.occupation)) {
            params.occupation = options.occupation;
        }
        if(!isUndefined(options.longitude) && isNumber(options.longitude)) {
            params.longitude = options.longitude;
        }
        if(!isUndefined(options.latitude) && isNumber(options.latitude)) {
            params.latitude = options.latitude;
        }
        if(!isUndefined(options.intentionLevel) && isNumber(options.intentionLevel)) {
            params.intentionLevel = options.intentionLevel;
        }
        if(!isUndefined(options.needType) && isNumber(options.needType)) {
            params.needType = options.needType;
        }
        if(!isUndefined(options.remark) && isString(options.remark)) {
            params.remark = options.remark;
        }
        if(!isUndefined(options.picUrl) && isString(options.picUrl)) {
            params.picUrl = options.picUrl;
        }
        return new Promise((resolve, reject) =>{
            request(MS._config.parameters.CUSTOMER_INFO.EMPLOYEE_ENTRY.url,
                MS._config.parameters.CUSTOMER_INFO.EMPLOYEE_ENTRY.method, params).then(res => {
                    resolve(res.data)
            }).catch(err =>{
                reject(err)
            })
        })
    }

    userScanEntry(options) {
        let params = {}
        if(!isUndefined(options.gathererId) && isString(options.gathererId)) {
            params.gathererId = options.gathererId;
        }
        if(!isUndefined(options.customerName) && isString(options.customerName)) {
            params.customerName = options.customerName;
        }
        if(!isUndefined(options.email) && isString(options.email)) {
            params.email = options.email;
        }
        if(!isUndefined(options.wechat) && isString(options.wechat)) {
            params.wechat = options.wechat;
        }
        if(!isUndefined(options.phone) && isString(options.phone)) {
            params.phone = options.phone;
        }
        if(!isUndefined(options.age) && isNumber(options.age)) {
            params.age = options.age;
        }
        if(!isUndefined(options.birthday) && isString(options.birthday)) {
            params.birthday = options.birthday;
        }
        if(!isUndefined(options.gender) && isNumber(options.gender)) {
            params.gender = options.gender;
        }
        if(!isUndefined(options.occupation) && isString(options.occupation)) {
            params.occupation = options.occupation;
        }
        if(!isUndefined(options.customerId) && isString(options.customerId)) {
            params.customerId = options.customerId;
        }
        if(!isUndefined(options.customerOpenId) && isString(options.customerOpenId)) {
            params.customerOpenId = options.customerOpenId;
        }
        if(!isUndefined(options.longitude) && isNumber(options.longitude)) {
            params.longitude = options.longitude;
        }
        if(!isUndefined(options.latitude) && isNumber(options.latitude)) {
            params.latitude = options.latitude;
        }
        if(!isUndefined(options.customerUnionId) && isString(options.customerUnionId)) {
            params.customerUnionId = options.customerUnionId;
        }
        if(!isUndefined(options.country) && isString(options.country)) {
            params.country = options.country;
        }
        if(!isUndefined(options.province) && isString(options.province)) {
            params.province = options.province;
        }
        if(!isUndefined(options.city) && isString(options.city)) {
            params.city = options.city;
        }
        if(!isUndefined(options.intentionLevel) && isNumber(options.intentionLevel)) {
            params.intentionLevel = options.intentionLevel;
        }
        if(!isUndefined(options.needType) && isNumber(options.needType)) {
            params.needType = options.needType;
        }
        return new Promise((resolve, reject) =>{
            request(MS._config.parameters.CUSTOMER_INFO.USER_SCAN_ENTRY.url,
                MS._config.parameters.CUSTOMER_INFO.USER_SCAN_ENTRY.method, params).then(res => {
                resolve(res.data)
            }).catch(err =>{
                reject(err)
            })
        })
    }

    checkExistRecord(options) {
        let params = {}
        if(!isUndefined(options)) {
            if(!isUndefined(options.wxUserId) && isString(options.wxUserId)) {
                params.wxUserId = options.wxUserId
            }
        }
        return new Promise((resolve, reject) =>{
            request(MS._config.parameters.CUSTOMER_INFO.CHECK_EXIST_RECORD.url,
                MS._config.parameters.CUSTOMER_INFO.CHECK_EXIST_RECORD.method, params).then(res => {
                resolve(res.data)
            }).catch(err =>{
                reject(err)
            })
        })
    }

    loadIntentionLevel() {
        return new Promise((resolve, reject) =>{
            request(MS._config.parameters.CUSTOMER_INFO.LOAD_INTENTION_LEVEL.url,
                MS._config.parameters.CUSTOMER_INFO.LOAD_INTENTION_LEVEL.method, {}).then(res => {
                    resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    loadNeedType() {
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.CUSTOMER_INFO.LOAD_NEED_TYPE.url,
                MS._config.parameters.CUSTOMER_INFO.LOAD_NEED_TYPE.method, {}).then(res => {
                    resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }
}

module.exports = customerInfo